<template>
  <div>
    <div class="header">
      <el-button
        type="primary"
        @click="dialogFormVisible = true"
        class="add"
        size="small"
        >新增
      </el-button>
    </div>
    <el-table :data="clublist" style="width: 100%" stripe border :default-sort = "{prop: 'date', order: 'descending'}">
      <el-table-column label="社团名称" width="260" prop="clubname">
      </el-table-column>
      <el-table-column label="社长" width="260" prop="name">
      </el-table-column>
      <el-table-column label="社团QQ群" width="260" prop="QQ">
      </el-table-column>
      <el-table-column label="创建日期" width="260" sortable prop="date">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增 -->
    <el-dialog title="新增社团" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="社团名称" :label-width="formLabelWidth">
              <el-input
                v-model="form.clubname"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="社长" :label-width="formLabelWidth">
              <el-input
                v-model="form.name"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="block">
              <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-date-picker
                  v-model="form.date"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社团QQ群" :label-width="formLabelWidth">
              <el-input
                v-model="form.QQ"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="社团简介" :label-width="formLabelWidth">
              <el-input
                type="textarea"
                :rows="5"
                placeholder="第一段文字(请分为三段介绍本社团)"
                v-model="form.content1"
                maxlength="150"
                show-word-limit
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="5"
                placeholder="第二段文字(请分为三段介绍本社团)"
                v-model="form.content2"
                maxlength="150"
                show-word-limit
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="5"
                placeholder="第三段文字(请分为三段介绍本社团)"
                v-model="form.content3"
                maxlength="150"
                show-word-limit
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClub">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改社团" :visible.sync="editFormVisible" width="40%">
      <el-form :model="form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="社团名称" :label-width="formLabelWidth">
              <el-input
                v-model="editform.clubname"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="社长" :label-width="formLabelWidth">
              <el-input
                v-model="editform.name"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="block">
              <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-date-picker
                  v-model="editform.date"
                  type="date"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社团QQ群" :label-width="formLabelWidth">
              <el-input
                v-model="editform.QQ"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="社团简介" :label-width="formLabelWidth">
              <el-input
                type="textarea"
                :rows="5"
                placeholder="第一段文字(请分为三段介绍本社团)"
                v-model="editform.content1"
                maxlength="150"
                show-word-limit
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="5"
                placeholder="第二段文字(请分为三段介绍本社团)"
                v-model="editform.content2"
                maxlength="150"
                show-word-limit
              >
              </el-input>
              <el-input
                type="textarea"
                :rows="5"
                placeholder="第三段文字(请分为三段介绍本社团)"
                v-model="editform.content3"
                maxlength="150"
                show-word-limit
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClub">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { v4 as uuidv4 } from "uuid";
export default {
  name: "MyClub",
  mounted() {
    this.all();
  },
  data() {
    return {
      imageUrl: "",
      deleteid: "",
      clublist: [],
      tableData: [],
      editFormVisible: false,
      dialogFormVisible: false,
      form: {
        id: "",
        clubname: "",
        name: "",
        date: "",
        QQ: "",
        content1: "",
        content2: "",
        content3: "",
      },
      editform: {
        id: "",
        clubname: "",
        name: "",
        date: "",
        QQ: "",
        content1: "",
        content2: "",
        content3: "",
      },
      editindex: "",
      formLabelWidth: "80px",
    };
  },
  methods: {
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editform = row;
      this.editindex = index;
    },
    handleDelete(index, row) {
      this.deleteid = row.id;
      console.log(this.deleteid);
      this.del();
    },
    addClub() {
      if (
        this.form.clubname == "" ||
        this.form.name == "" ||
        this.form.date == "" ||
        this.form.QQ == "" ||
        this.form.content1 == ""
      ) {
        alert(
          "请填写完整信息(社团名称、社长姓名、立社日期、社团QQ群、社团介绍)后再点击确定"
        );
      } else {
        this.dialogFormVisible = false;
        this.form.id = uuidv4();
        this.add();
        this.form = {
          id: "",
          clubname: "",
          name: "",
          date: "",
          QQ: "",
          content1: "",
          content2: "",
          content3: "",
        };
      }
    },
    editClub() {
      if (
        this.editform.clubname == "" ||
        this.editform.name == "" ||
        this.editform.date == "" ||
        this.editform.QQ == "" ||
        this.editform.content1 == ""
      ) {
        alert(
          "请填写完整信息(社团名称、社长姓名、立社日期、社团QQ群、社团介绍)后再点击确定"
        );
      } else {
        this.update();
        this.editFormVisible = false;
      }
    },
    all() {
      //查找info表全部数据
      axios
        .get("http://127.0.0.1/clublist_yang/all")
        .then((res) => {
          // console.log(res.data);
          this.clublist = res.data;
          // console.log(this.clublist);
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
    add() {
      //添加操作
      axios
        .get("http://127.0.0.1/clublist_yang/add", {
          params: {
            id: this.form.id,
            name: this.form.name.trim(),
            clubname: this.form.clubname.trim(),
            QQ: this.form.QQ.trim(),
            content1: this.form.content1.trim(),
            content2: this.form.content2.trim(),
            content3: this.form.content3.trim(),
            date: this.form.date.trim(),
          },
        })
        .then((res) => {
          console.log(res);
          this.all();
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
    del() {
      //删除操作
      axios
        .get("http://127.0.0.1/clublist_yang/del", {
          params: {
            id: this.deleteid,
          },
        })
        .then((res) => {
          console.log(res.data);
          this.all();
        })
        .catch((err) => {
          console.log("操作失败" + err);
        });
    },
    update() {
      //修改操作
      axios
        .get("http://127.0.0.1/clublist_yang/update", {
          params: {
            id: this.editform.id,
            name: this.editform.name.trim(),
            clubname: this.editform.clubname.trim(),
            QQ: this.editform.QQ.trim(),
            content1: this.editform.content1.trim(),
            content2: this.editform.content2.trim(),
            content3: this.editform.content3.trim(),
            date: this.editform.date.trim(),
          },
        })
        .then((res) => {
          console.log(res);
          this.all();
        })
        .catch((err) => {
          console.log("获取数据失败" + err);
        });
    },
  },
};
</script>

<style scoped>
.header {
  height: 56px;
  width: 100%;
  background-color: #1e874c;
}
.add {
  margin-top: 10px;
  margin-left: 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
